<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
    <title>15-兄弟组件之间传值</title>
    <style>
        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body>
<div id="app" v-cloak></div>
<script src="js/vue.js"></script>
<script>
    //定义一个bus总线
    let bus = new Vue;
    //a组件
    let Acomponent = {
        template: `
            <div>
                我是A组件
                <button type="button" v-on:click="sendB()">给B组件传值</button>
            </div>
        `,
        data() {
            return {}
        },
        methods: {
            sendB() {
                bus.$emit("getA", "A组件传过来的值");
            }
        }
    };

    //b组件
    let Bcomponent = {
        template: `
            <div>
                我是b组件,a组件的值：{{aVal}}
            </div>
        `,
        data() {
            return {
                aVal: ""
            }
        },
        created() {
            bus.$on("getA", (message) => {
                console.log(message);
                this.aVal = message;
            });
        }
    };
    //入口组件
    let App = {
        template: `
            <div>
                <Acomponent></Acomponent>
                <Bcomponent></Bcomponent>
            </div>
        `,
        //局部组件注册方式
        components: {
            Acomponent,
            Bcomponent
        }
    };
    new Vue({
        el: "#app",
        template: `
            <App></App>
        `,
        components: {
            App
        }
    })
</script>
</body>
</html>
